package com.platform.server.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 * 数字工具类
 * </p>
 * 
 */
public class NumberUtil {
	private static boolean isMatch(String regex, String orginal) {
		if (orginal == null || orginal.trim().equals("")) {
			return false;
		}
		Pattern pattern = Pattern.compile(regex);
		Matcher isNum = pattern.matcher(orginal);
		return isNum.matches();
	}

	/** 判断一个字符串是否正整数 */
	public static boolean isPositiveInteger(String orginal) {
		return isMatch("^\\+{0,1}\\d+", orginal);
	}

	/** 判断一个字符串是否负整数 */
	public static boolean isNegativeInteger(String orginal) {
		return isMatch("^-\\d+", orginal);
	}

	/** 判断一个字符串是否整数 */
	public static boolean isWholeNumber(String orginal) {
		return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
	}

	/** 判断一个字符串是否正浮点数 */
	public static boolean isPositiveDecimal(String orginal) {
		return isMatch("\\+{0,1}\\d+\\.\\d*|\\+{0,1}\\d*\\.\\d+", orginal);
	}

	/** 判断一个字符串是否负浮点数 */
	public static boolean isNegativeDecimal(String orginal) {
		return isMatch("^-\\d+\\.\\d*|^-\\d*\\.\\d+", orginal);
	}

	/** 判断一个字符串是否浮点数 */
	public static boolean isDecimal(String orginal) {
		return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
	}

	/** 判断一个字符串是否数字 */
	public static boolean isRealNumber(String orginal) {
		return isWholeNumber(orginal) || isDecimal(orginal);
	}

	/**
	 * 保留指定位数小数点
	 * 
	 * @param number
	 * @param precision
	 * @return
	 */
	public static double round(double number, int precision) {
		return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision);
	}

	public static void main(String[] args) {
		System.out.println(NumberUtil.round(0.02984, 1));
	}
}
